
const app = { middlewares: [] };

app.use = (fn) => {
    app.middlewares.push(fn);
};
app.compose = function (...fns) {
    //  Your Code Here

    const middleware = [...fns];
    // console.log(middleware);
    return dispatch(0)

    function dispatch(index) {
        const fn = middleware[index]
        if (!fn) {
            return Promise.resolve("结束了");
        }
        // console.log("执行：", index)
        return Promise.resolve(fn(function next() {
            return dispatch(index + 1)
        }))
    }

}
app.use(next => {
    console.log(1);
    next();
    console.log(2);
});
app.use(next => {
    console.log(3);
    next();
    console.log(4);
});
app.use(next => {
    console.log(5);
    next();
    console.log(6);
});
app.compose();


// test1()
// function test1(params) {
//     console.log("哈哈哈");
// }

// test1()
// function test1(params) {
//     console.log("嘿嘿嘿");
// }
// test2()
// let a = '100'
// function test2(params) {
//     console.log(a)
//     let a = '10'
// }